<?php
class Controller_Admin_Recommend extends Controller_Template 
{
	protected $_limit = '';
	protected $_types = '';
	private $_sphinx  = '';
	private $_recommend = '';
	public function __construct($request, $response)
	{
		$this->_limit	= 20;

		$this->_types	= Kohana::config('brand.home_caterecommend');
		$this->template = 'smarty:admin/home/default';
		$this->_recommend = new Model_Recommend();
		$this->_sphinx	= new Model_Sphinx();
		parent::__construct($request, $response);
	}

	public function action_index()
	{

		$this->action_brand();
		exit;
	}

	public function action_brand($status = null)
	{

		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);
	
		$datas	= '';

		$status	= strval($status);
	
		if(empty($status))
			$status	= 'furniture';
		$s_val	= $this->_types[$status];
		$count	= 0;	
		
		$type	= Kohana::config('brand.recommend_type.brand');

		$rs		= Model_Recommend::get_typeinfo($type, $s_val[0]);

		$count	= count($rs);

		$ids	= array();
		for($i=0; $i< $count; $i++)
		{
			$ids[]	= $rs[$i]['obj_id'];
		}
		$brand_info	= '';
		if(!empty($ids))
			$brand_info	= Category_Brand::get_brands_logo($ids);

		$domain	= Site::domain('member');

		$counts	= count($rs);
		$rs_data= '';
		$k		= 0;
		//print_r($brand_info);
		for($i=0; $i< $count; $i++)
		{
			$rs_data[$k]['apply_uname']		= isset($brand_info[$i]['apply_uid']) ? User::name($brand_info[$i]['apply_uid']) : '';
			
			$rs_data[$k]['num']				= $rs[$i]['num'];

			$rs_data[$k]['create_time']		= date('Y-m-d', $rs[$i]['create_time']);

			$rs_data[$k]['name']			= $brand_info[$i]['name'];

			$rs_data[$k]['logo_url']        = $brand_info[$i]['logo_url'];

			$rs_data[$k]['id']				= $rs[$i]['obj_id'];

			$rs_data[$k]['recommend_id']    = $rs[$i]['id'];
			$k++;
		}
		
		$page_config = array(
							'total_items' => $count,
							'items_per_page' => $this->_limit,
						);
		$pagination		= Pagination::factory($page_config);
		
		$this->template->set_filename('smarty:admin/home/brandrecommend');
		$this->template->set('pagination', $pagination->render());
		$this->template->set('total', $count);
		$this->template->set('data', $rs_data);
		$this->template->set('status', $status);
		$this->template->set('status_name', $status);
		$this->template->set('function_name', 'brand_recommend');
		$this->template->set('page_title', '首页品牌推荐列表');
		$this->response->body($this->template->render());

	}

	public function action_setbrand($status = null)
	{

		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);
	
		$datas	= '';

		$status	= strval($status);
	
		if(empty($status))
			$status	= 'furniture';
		$s_val	= $this->_types[$status];
		$count	= 0;	
		if($status != 'home')//二级分类
		{
			$datas	= Category_Brand::get_brand_by_categorys($s_val, $this->_limit, $offset);	
			
			$count		= Category_Brand::count_brand_by_categorys($s_val);
		}else
		{
			$datas	= Category_Brand::get_all_brand($this->_limit, $offset);
		
			$count  = Category_Brand::count_all_brand(0,0); 
		}
	
		$type		= Kohana::config('brand.recommend_type.brand');
		$rs	= '';
		
		$brand_info	= Model_Recommend::get_typeinfo($type, $s_val[0]);
		
		$ids	= array();
		for($k=0; $k < count($brand_info); $k++)
		{
			$ids[]	= $brand_info[$k]['obj_id'];	
		}
		
		for($i=0;$i< count($datas); $i++)
		{
			if(! in_array($datas[$i]['id'], $ids))
			{
				$rs[$i] = $datas[$i];
				
				$rs[$i]['apply_uname']	= isset($datas[$i]['apply_uid']) ? User::name($datas[$i]['apply_uid']) : '';
				
				$rs[$i]['create_time']	= date('Y-m-d', $datas[$i]['create_time']);
			}
		}
		$page_config = array(
							'total_items' => $count,
							'items_per_page' => $this->_limit,
						);
		$pagination		= Pagination::factory($page_config);
		
		$this->template->set_filename('smarty:admin/home/setbrandrecommend');
		$this->template->set('pagination', $pagination->render());
		$this->template->set('total', $count);
		$this->template->set('data', $rs);
		$this->template->set('status', $status);
		$this->template->set('status_name', 's_'.$status);
		$this->template->set('function_name', 'brand_recommend');
		$this->template->set('page_title', '首页品牌推荐列表');
		$this->response->body($this->template->render());

	}

	public function action_addbrand()
	{
		
		$num		= intval($this->request->post('num'));
		$status		= strval($this->request->post('brand_status'));
		$brand_id	= intval($this->request->post('brand_id'));

		$type		= Kohana::config('brand.recommend_type.brand');
		
		$s_val	= $this->_types[$status];
		
		if($type < 0 || $brand_id <= 0 || $num <=0 || !isset($s_val))
		{
			echo '参数错误！';

			exit;
		}

		$rs	= $this->updategoods($brand_id, $type, $s_val[0], $num);

		echo '修改成功！';

		exit;

	}

	public function action_goods($status)
	{
		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);
	
		$status	= strval($status);

		$s_val	= $this->_types[$status];
		
		$type	= Kohana::config('brand.recommend_type.goods');

		$rs		= Model_Recommend::get_typeinfo($type, $s_val[0]);

		$domain	= Site::domain('member');

		$counts	= count($rs);
		$rs_data= '';
		$k		= 0;
		for($i=0; $i< $counts; $i++)
		{
			$query['id']	= $rs[$i]['obj_id'];

			$id				= $query['id'];

			//$query['status']= 'valid';

			//$query['shop_status']= 'valid';

			$goods_info		= Goods::search($query, 0, 1);
			
			if(empty($goods_info['list'][$id]))
				continue;

			$rs_data[$k]['price']		 = $goods_info['list'][$id]['price'];

			$rs_data[$k]['title']		 = $goods_info['list'][$id]['title'];
			
			$rs_data[$k]['shop_name']    = Shop::name($goods_info['list'][$id]['shop_id']);

			$rs_data[$k]['goods_url']    = 'http://'.$domain.'/shop/goods/'.$goods_info['list'][$id]['goods_id'];

			$rs_data[$k]['shop_url']     = 'http://'.$domain.'/shop/index/'.$goods_info['list'][$id]['shop_id'];

			$rs_data[$k]['create_time']  = date('Y-m-d', $goods_info['list'][$id]['create_time']);

			$rs_data[$k]['num']			 = $rs[$i]['num'];

			$rs_data[$k]['goods_id']	 = $rs[$i]['obj_id'];

			$rs_data[$k]['recommend_id']     = $rs[$i]['id'];
			$k++;
		}
		$page_config	= array(
								'total_items'		=> $counts,
								'items_per_page'	=> $this->_limit,
							);

		$pagination		= Pagination::factory($page_config);

		$this->template->set_filename('smarty:admin/home/goodsrecommend');
		$this->template->set('pagination', $pagination->render());
		$this->template->set('total', $counts);
		$this->template->set('data', $rs_data);
		//$this->template->set('rs_data', $rs_datas);
		$this->template->set('type_name', $status);
		$this->template->set('type', $status);
		$this->template->set('function_name', 'goods_recommend');
		$this->template->set('page_title', '商品推荐');
		$this->response->body($this->template->render());
	}

	public function action_setgoods($status)
	{
		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);
	
		$datas	= '';

		$status	= strval($status);

		$s_val	= $this->_types[$status];
		$query	= '';
		if($status	== 'textiles')
		{
			//$query	= array('category_sys_2' => $s_val, 'order_by' => array('id' => 'desc'));
			
			$query  = array('category_sys_1' => $this->_types['appliances'], 'order_by' => array('id' => 'desc'));

		}else
			$query  = array('category_sys_1' => $s_val, 'order_by' => array('id' => 'desc'));
		
		$datas	= '';

		$query	= '';

		$query['status']= 'valid';

		$query['shop_status']= 'valid';

		if(!empty($s_val))
			$datas	= Goods::search($query, $offset, $this->_limit);
		
		$count	= 0;
		$data	= '';
		if(!empty($datas))
		{
			$count	= $datas['count'];

			$data	= $datas['list'];
		}
		$domain	= Site::domain('member');
		
		$type		= Kohana::config('brand.recommend_type.goods');

		$datas		= '';

		$k = 0;
		if($count> 0)
		{
			foreach($data as $key => $val)
			{
				$info							=  Model_Recommend::get_num($type, $s_val[0], $data[$key]['goods_id']);

				if(empty($info))
				{
			
					$datas[$k]['goods_id']         = $data[$key]['goods_id'];

					$datas[$k]['title']            = $data[$key]['title'];

					$datas[$k]['price']            = $data[$key]['price'];

					$datas[$k]['shop_name']			= Shop::name($data[$key]['shop_id']);

					$datas[$k]['goods_url']			= 'http://'.$domain.'/shop/goods/'.$data[$key]['goods_id'];

					$datas[$k]['shop_url']			= 'http://'.$domain.'/shop/index/'.$data[$key]['shop_id'];

					$datas[$k]['create_time']		= date('Y-m-d', $data[$key]['create_time']);
			
					$datas[$k]['num']               = empty($info) ? '' : $info['num'];

					$datas[$k]['recommend_id']      = empty($info) ? '' : $info['id'];

					$k++;
				}
			}	
		}
		$page_config	= array(
								'total_items'		=> $count,
								'items_per_page'	=> $this->_limit,
							);

		$pagination		= Pagination::factory($page_config);

		//print_r($rs_datas);
		$this->template->set_filename('smarty:admin/home/setgoodsrecommend');
		$this->template->set('pagination', $pagination->render());
		$this->template->set('total', $count);
		$this->template->set('data', $datas);
		$this->template->set('type_name', 's_'.$status);
		$this->template->set('type', $status);
		$this->template->set('function_name', 'goods_recommend');
		$this->template->set('page_title', '商品推荐');
		$this->response->body($this->template->render());

	}

	public function action_addgoods()
	{

		$num		= intval($this->request->post('num'));
		$status		= $this->request->post('good_type');
		$goods_id	= intval($this->request->post('goods_id'));

		if(is_int($status))
			$status	= $this->home_caterecommend($status);
		else
			$status	= strval($status);

		$type		= Kohana::config('brand.recommend_type.goods');
		//echo $num.'|'.$type,'|',$goods_id,'|',$status;exit;
	
		$s_val	= $this->_types[$status];

		if($type <= 0 || $goods_id <= 0 || !isset($s_val) || intval($s_val[0]) <=0 || $num <=0)
		{
			echo '参数错误！';

			exit;
		}
		
		$rs	= $this->updategoods($goods_id, $type, $s_val[0], $num);
		
		echo '修改成功！';

		exit;
	}

	public function action_subject($status = null)
	{
		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);
		
		$sub_type = Kohana::config('subject.subject_type');

		$status	= $this->request->query('subject_type_name');
		
		if(! empty($status))
			$subject_status	= $sub_type[$status];
		
		$type	= Kohana::config('brand.recommend_type.subject');
		
		$rs		= Model_Recommend::get_typeinfo($type, $subject_status, $offset, $this->_limit);
		
		$domain	= Site::domain('member');

		$counts	= Model_Recommend::count_typeinfo($type, $subject_status);
		
		$rs_data= $this->subject_dataformate($rs);

		$page_config	= array(
								'total_items'		=> $counts,
								'items_per_page'	=> $this->_limit,
							);

		$pagination		= Pagination::factory($page_config);
		$this->template->set_filename('smarty:admin/home/subjectgoods');
		$this->template->set('pagination', $pagination->render());
		$this->template->set('total', $counts);
		$this->template->set('data', $rs_data);
		$this->template->set('subject_type', Kohana::config('subject.subject_typename'));
		$this->template->set('type', $status);
		$this->template->set('function_name', 'subject');
		$this->template->set('page_title', '商品推荐');
		$this->response->body($this->template->render());
	}

	private function subject_dataformate($rs)
	{
		$rs_data= array();
		$k		= 0;
		$domain	= Site::domain('member');
		for($i=0; $i< count($rs); $i++)
		{
			$query['id']	= $rs[$i]['obj_id'];

			$id				= $query['id'];

			$goods_info		= Goods::search($query, 0, 1);
			
			if(empty($goods_info['list'][$id]))
				continue;
			$rs_data[$k]['price']		 = $goods_info['list'][$id]['price'];
			
			if(intval($goods_info['list'][$id]['promote_price']) > 0)
				$rs_data[$k]['price']    = $goods_info['list'][$id]['promote_price'];

			$rs_data[$k]['title']		 = $goods_info['list'][$id]['title'];
			
			$rs_data[$k]['shop_name']    = Shop::name($goods_info['list'][$id]['shop_id']);

			$rs_data[$k]['goods_url']    = 'http://'.$domain.'/shop/goods/'.$goods_info['list'][$id]['goods_id'];

			$rs_data[$k]['shop_url']     = 'http://'.$domain.'/shop/index/'.$goods_info['list'][$id]['shop_id'];

			$rs_data[$k]['create_time']  = date('Y-m-d', $goods_info['list'][$id]['create_time']);

			$rs_data[$k]['num']			 = $rs[$i]['num'];

			$rs_data[$k]['goods_id']	 = $rs[$i]['obj_id'];

			$rs_data[$k]['desc']		 = $rs[$i]['desc'];

			$rs_data[$k]['fare']		 = $rs[$i]['fare'];

			if($rs[$i]['price'] > 0)
				$rs_data[$k]['price']         = sprintf('%0.02f', $rs[$i]['price']/100);

			$rs_data[$i]['pic_url']		 = $rs[$i]['pic_url'];

			$arr_status					 = $this->get_subject_status($rs[$i]['status']);

			$rs_data[$k]['status']		 = $arr_status['status'];

			$rs_data[$k]['status_name']	 = $arr_status['value'];

			$rs_data[$k]['recommend_id'] = $rs[$i]['id'];
			$k++;
		}
		return $rs_data;
	}

	private function get_subject_status($status)
	{
		$status_value	= Kohana::config('subject.subject_type');

		$type_name		= Kohana::config('subject.subject_typename');

		foreach($status_value as $key => $val)
		{
			if($val == $status)
				return array('status' => $key, 'value' => $type_name[$key]);
		}
	
	}

	public function action_addsubject()
	{

		$num		= intval($this->request->post('num'));
		$type		= $this->request->post('subject_type');
		$goods_id	= intval($this->request->post('goods_id'));
		$arr['title']	= strval($this->request->post('title'));
		$arr['price']	= floatval($this->request->post('price')) * 100;
		$arr['pic']		= strval($this->request->post('pic_url'));
		$arr['fare']	= strval($this->request->post('fare'));

		$types		= Kohana::config('subject.subject_type');
		
		$status 	= $types[$type];
		
		$type		= Kohana::config('brand.recommend_type.subject');
		
		if($status<= 0 || $goods_id <= 0 || $type<=0 || $num <=0)
		{
			echo '参数错误！';

			exit;
		}
		
		$rs	= $this->updategoods($goods_id, $type, $status, $num, $arr);

		echo '修改成功！';

		exit;
	}

	protected function home_caterecommend($category_sys)
	{
		$category	= intval($category_sys);

		if($category <=0 )
			return null;

		$cates		= Category_System::parent_ids($category);

		$rs		= $this->get_recommendtype($cates);
	}

	private function get_recommendtype($cates)
	{
		$recommendtypes	= Kohana::config('brand.home_caterecommend');

		$recommend_type	= '';

		for($i=0; $i< 2; $i++)
		{
			foreach($recommendtypes as $key => $val)
			{
				if(in_array($cates[$i], $recommendtypes[$key]))
				{		
					$recommend_type	= $key;
				
					break;
				}
			}

		}
		
		return $recommend_type;
	}

	public function action_delrecommend()
	{
		$id	= $this->request->post("del_id");
	
		if(intval($id) <= 0)
		{
			echo '参数错误！';exit;
		}
		
		$rs = $this->_recommend->del($id);

		Cache::instance()->delete('subject_cache');

		echo '删除成功!';

		exit;
	}


	protected function updategoods($goods_id, $type, $status, $num, $info = array())
	{
		$goods_id	= intval($goods_id);
		$type		= intval($type);
		$num		= intval($num);
		$status		= intval($status);
		
		if($type <= 0 || $goods_id <= 0)
			return false;
	
		Cache::instance()->delete('subject_cache');

		$rs	= $this->_recommend->update_sort($goods_id, $type, $status, $num, $info);

		return $rs;
	}
}

?>
